package topo.dialog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import topo.data.NodeDevice;
import topo.data.NodeType;
import topo.service.TopoDataManager;
import topo.utils.CheckIPUtil;
import topo.utils.DialogTools;

public class ProxyDialog extends JDialog {
    static ResourceBundle res = ResourceBundle.getBundle(ProxyDialog.class.getPackage().getName() + ".Res");

    JPanel panel1 = new JPanel();

    JLabel jLabel1 = new JLabel();

    JTextField jTextFieldName = new JTextField();

    JLabel jLabel3 = new JLabel();

    JTextField jTextFieldIP = new JTextField();

    JLabel jLabel4 = new JLabel();

    JTextField jTextFieldRead = new JTextField();

    JLabel jLabel5 = new JLabel();

    JTextField jTextFieldWrite = new JTextField();

    JTextField jTextFieldproxyIp = new JTextField();
    JLabel jLabel7 = new JLabel();

    JLabel jLabel8 = new JLabel();

    JLabel jLabel9 = new JLabel();

    JLabel jLabel10 = new JLabel();

    JLabel portLabel = new JLabel();

    JLabel jLabelPort = new JLabel();
    JLabel jLabeSnmpProxydes = new JLabel();
    JTextField jTextFieldPort = new JTextField();

    BorderLayout borderLayout1 = new BorderLayout();

    JPanel jPanel1 = new JPanel();

    JButton jButtonAdd = new JButton();

    JButton jButtonClose = new JButton();

    JLabel jLabel11 = new JLabel();

    private Border border3 = BorderFactory.createEtchedBorder(Color.white, new Color(148, 145, 140));

    private Border border4 = new TitledBorder(this.border3, res.getString("String_132"));

    JLabel jLabel2 = new JLabel();

    JLabel jLabeSnmpProxy = new JLabel();
    JLabel jLabeProxyIp = new JLabel();
    JLabel jLabeSnmpV = new JLabel();

    JComboBox jComboBoxType = new JComboBox();

    JComboBox jComboBoxSnmpProxy = new JComboBox();
    JComboBox jComboBoxSnmpV = new JComboBox();

    public void myInit() {
        List list = NodeType.getNodeTypeList();
        for (int i = 0; i < list.size(); i++) {
            NodeType type = (NodeType) list.get(i);
            this.jComboBoxType.addItem(type.getNodeType());
        }
        this.jComboBoxType.setEditable(false);
        this.jTextFieldproxyIp.setEnabled(false);
    }

    public ProxyDialog(Frame frame) {
        super(frame, res.getString("String_133"), true);
        try {
            setSize(387, 490);
            jbInit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        setDefaultCloseOperation(2);
        setTitle(res.getString("String_150"));
        getContentPane().setLayout(this.borderLayout1);

        this.panel1.setLayout(null);
        this.jLabel1.setText(res.getString("String_138"));
        this.jLabel1.setBounds(new Rectangle(34, 21, 75, 20));
        this.jTextFieldName.setFont(new Font("Dialog", 0, 13));
        this.jTextFieldName.setAlignmentY(0.5F);
        this.jTextFieldName.setText(res.getString("String_140"));
        this.jTextFieldName.setBounds(new Rectangle(155, 21, 105, 25));

        this.jLabel3.setText(res.getString("String_141"));
        this.jLabel3.setBounds(new Rectangle(34, 66, 89, 20));
        this.jTextFieldIP.setAlignmentY(0.5F);
        this.jTextFieldIP.setText("192.1.1.1");
        this.jTextFieldIP.setBounds(new Rectangle(155, 66, 105, 25));

        this.jLabel11.setText("          ");
        this.jLabel2.setText(res.getString("String_166"));
        this.jLabel2.setBounds(new Rectangle(34, 111, 89, 16));
        this.jComboBoxType.setBounds(new Rectangle(156, 111, 105, 25));

        this.jLabel4.setText("SNMP读密码");
        this.jLabel4.setBounds(new Rectangle(34, 156, 114, 20));
        this.jLabel4.setToolTipText("");
        this.jTextFieldRead.setText("public");
        this.jTextFieldRead.setBounds(new Rectangle(155, 156, 105, 25));
        this.jTextFieldRead.setAlignmentY(0.5F);

        this.jLabel5.setRequestFocusEnabled(true);
        this.jLabel5.setText("SNMP写密码");
        this.jLabel5.setBounds(new Rectangle(34, 201, 114, 20));
        this.jTextFieldWrite.setAlignmentY(0.5F);
        this.jTextFieldWrite.setText("public");
        this.jTextFieldWrite.setBounds(new Rectangle(155, 201, 105, 25));

        this.jLabelPort.setText("SNMP端口:");
        this.jLabelPort.setBounds(new Rectangle(34, 246, 105, 25));
        this.jTextFieldPort.setEnabled(true);
        this.jTextFieldPort.setText("161");
        this.jTextFieldPort.setBounds(new Rectangle(155, 246, 105, 25));
        this.portLabel.setForeground(Color.red);
        this.portLabel.setHorizontalAlignment(0);
        this.portLabel.setHorizontalTextPosition(0);
        this.portLabel.setText("*");
        this.portLabel.setBounds(new Rectangle(267, 246, 9, 19));

        this.jLabeSnmpV.setText("SNMP版本：");
        this.jLabeSnmpV.setBounds(new Rectangle(34, 291, 89, 16));
        this.jComboBoxSnmpV.setBounds(new Rectangle(156, 291, 105, 25));
        this.jComboBoxSnmpV.addItem("V2C");
        this.jComboBoxSnmpV.addItem("V1");

        this.jLabeSnmpProxy.setText("启用分布式SNMP代理：");
        this.jLabeSnmpProxy.setBounds(new Rectangle(24, 336, 149, 16));
        this.jComboBoxSnmpProxy.setBounds(new Rectangle(156, 336, 105, 25));
        this.jComboBoxSnmpProxy.addItem("启用");
        this.jComboBoxSnmpProxy.addItem("禁用");
        this.jComboBoxSnmpProxy.setSelectedIndex(1);
        this.jComboBoxSnmpProxy.addItemListener(new CommProxyDialog_jComboBoxSnmpProxy_itemAdapter(this));
        this.jLabeSnmpProxydes.setText("启用分布式SNMP代理，可以通过代理服务器对设备进行监控");
        this.jLabeSnmpProxydes.setBounds(new Rectangle(24, 364, 350, 16));

        this.jLabeProxyIp.setText("代理服务器IP：");
        this.jLabeProxyIp.setBounds(new Rectangle(34, 384, 89, 16));
        this.jTextFieldproxyIp.setText("127.0.0.1");
        this.jTextFieldproxyIp.setBounds(new Rectangle(155, 384, 105, 25));

        this.jLabel7.setForeground(Color.red);
        this.jLabel7.setRequestFocusEnabled(true);
        this.jLabel7.setHorizontalAlignment(0);
        this.jLabel7.setHorizontalTextPosition(0);
        this.jLabel7.setText("*");
        this.jLabel7.setBounds(new Rectangle(267, 25, 9, 19));

        this.jLabel8.setForeground(Color.red);
        this.jLabel8.setText("*");
        this.jLabel8.setBounds(new Rectangle(267, 66, 9, 19));
        this.jLabel8.setHorizontalTextPosition(0);
        this.jLabel8.setHorizontalAlignment(0);

        this.jLabel9.setForeground(Color.red);
        this.jLabel9.setHorizontalAlignment(0);
        this.jLabel9.setHorizontalTextPosition(0);
        this.jLabel9.setText("*");
        this.jLabel9.setBounds(new Rectangle(267, 156, 9, 19));

        this.jLabel10.setForeground(Color.red);
        this.jLabel10.setHorizontalAlignment(0);
        this.jLabel10.setHorizontalTextPosition(0);
        this.jLabel10.setText("*");
        this.jLabel10.setBounds(new Rectangle(267, 201, 9, 19));

        this.panel1.setBorder(this.border4);
        this.jButtonAdd.setText(res.getString("String_163"));
        this.jButtonAdd.addActionListener(new CommProxyDialog_jButtonAdd_actionAdapter(this));

        this.jButtonClose.setText(res.getString("String_164"));
        this.jButtonClose.addActionListener(new CommProxyDialog_jButtonClose_actionAdapter(this));

        this.panel1.add(this.jTextFieldWrite, null);
        this.panel1.add(this.jLabel1, null);
        this.panel1.add(this.jTextFieldName, null);
        this.panel1.add(this.jTextFieldRead, null);
        this.panel1.add(this.jTextFieldIP, null);
        this.panel1.add(this.jLabel3, null);
        this.panel1.add(this.jLabel4, null);
        this.panel1.add(this.jLabel5, null);
        this.panel1.add(this.jLabel7, null);
        this.panel1.add(this.jLabel8, null);
        this.panel1.add(this.jLabel9, null);
        this.panel1.add(this.jLabel10, null);
        this.panel1.add(this.portLabel, null);

        this.panel1.add(this.jLabeSnmpV, null);
        this.panel1.add(this.jLabeSnmpProxy, null);
        this.panel1.add(this.jLabeProxyIp, null);

        this.panel1.add(this.jComboBoxSnmpV, null);
        this.panel1.add(this.jComboBoxSnmpProxy, null);
        this.panel1.add(this.jLabeSnmpProxydes, null);
        this.panel1.add(this.jTextFieldproxyIp, null);
        this.panel1.add(this.jComboBoxType);
        this.panel1.add(this.jLabel2, null);
        this.panel1.add(this.jLabelPort, null);
        this.panel1.add(this.jTextFieldPort, null);
        getContentPane().add(this.jPanel1, "South");
        this.jPanel1.add(this.jButtonAdd, null);
        this.jPanel1.add(this.jLabel11, null);
        this.jPanel1.add(this.jButtonClose, null);

        getContentPane().add(this.panel1, "Center");
    }

    void jButtonClose_actionPerformed(ActionEvent e) {
        dispose();
    }

    public void jButtonAdd_actionPerformed(ActionEvent e) {
        try {
            NodeDevice db = new NodeDevice();
            if ("".equals(this.jTextFieldName.getText().trim())) {
                DialogTools.showErroMsg(this, "请输入设备名称!");
                return;
            }
            String ip = this.jTextFieldIP.getText().trim();
            if ((ip == null) || (ip.equals(""))) {
                DialogTools.showErroMsg(this, "请输入设备IP地址!");
                return;
            }

            if (!CheckIPUtil.checkIP(ip)) {
                DialogTools.showErroMsg(this, "设备IP地址输入错误,请重新输入!");
                this.jTextFieldIP.grabFocus();
                return;
            }

            if (this.jComboBoxType.getSelectedItem() == null) {
                DialogTools.showErroMsg(this, "请选择设备类型!");
                return;
            }
            if ("".equals(this.jTextFieldRead.getText().trim())) {
                DialogTools.showErroMsg(this, "请输入SNMP读共同体!");
                return;
            }
            if ("".equals(this.jTextFieldWrite.getText().trim())) {
                DialogTools.showErroMsg(this, "请输入SNMP写共同体!");
                return;
            }
            if ("".equals(this.jTextFieldPort.getText().trim())) {
                DialogTools.showErroMsg(this, "请输入SNMP端口号!");
                return;
            }
            if (TopoDataManager.getInstance().checkExistDeviceByIP(ip)) {
                DialogTools.showErroMsg(this, "当前拓扑已存在该IP的设备,不能重复添加!");
                return;
            }

            db.setIpAddress(ip);
            db.setReadCommit(this.jTextFieldRead.getText());
            db.setWriteCommit(this.jTextFieldWrite.getText());

            db.setNodeType(this.jComboBoxType.getSelectedItem().toString());
            db.setDisplayName(this.jTextFieldName.getText());
            db.setSnmpPort(new Integer(this.jTextFieldPort.getText()).intValue());
            db.setNodeName(db.genKey());

            if (this.jComboBoxSnmpV.getSelectedIndex() == 0) {
                db.setSnmpVersion(1);
            } else if (this.jComboBoxSnmpV.getSelectedIndex() == 1) {
                db.setSnmpVersion(0);
            }

            int isEnableSNMPProxy = this.jComboBoxSnmpProxy.getSelectedIndex();
            if (isEnableSNMPProxy == 0) {
                String proxyIP = this.jTextFieldIP.getText().trim();
                if ((proxyIP == null) || (proxyIP.equals(""))) {
                    DialogTools.showErroMsg(this, "请输入代理服务器IP地址!");
                    return;
                }

                if (!CheckIPUtil.checkIP(proxyIP)) {
                    DialogTools.showErroMsg(this, "代理服务器IP地址输入错误,请重新输入!");
                    this.jTextFieldIP.grabFocus();
                    return;
                }

                db.setIsEnableSNMPProxy(1);
            } else if (this.jComboBoxSnmpProxy.getSelectedIndex() == 1) {
                db.setIsEnableSNMPProxy(0);
            }
            db.setSnmpProxyIP(this.jTextFieldproxyIp.getText().trim());

            TopoDataManager.getInstance().addNode(db);
            dispose();
        } catch (Exception ex) {
            DialogTools.showErroMsg(this, res.getString("String_167"));
            ex.printStackTrace();
        }
    }

    void jComboBoxSnmpProxy_itemStateChanged(ItemEvent e) {
        if (this.jComboBoxSnmpProxy.getSelectedIndex() == 0) {
            this.jTextFieldproxyIp.setEnabled(true);
        } else {
            this.jTextFieldproxyIp.setEnabled(false);
        }
    }

    class CommProxyDialog_jComboBoxSnmpProxy_itemAdapter implements ItemListener {
        ProxyDialog adaptee;

        CommProxyDialog_jComboBoxSnmpProxy_itemAdapter(ProxyDialog adaptee) {
            this.adaptee = adaptee;
        }

        public void itemStateChanged(ItemEvent e) {
            this.adaptee.jComboBoxSnmpProxy_itemStateChanged(e);
        }
    }
}